﻿using NewWms.Apiservice.Dto;
using OfficeOpenXml;
using System.ComponentModel;
using System.IO;
using LicenseContext = OfficeOpenXml.LicenseContext;

namespace wms_wpf.Extend
{
    public static class ExportToExcel
    {

        public static void ExportToExcelMethod(List<View_Material_WarehouseLineDto> list)
        {
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string filePath = Path.Combine(desktopPath, "物料信息.xlsx");

            // 检查文件是否已经存在，如果存在则添加时间戳以区分文件
            if (File.Exists(filePath))
            {
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
                string fileExtension = Path.GetExtension(filePath);
                string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
                filePath = Path.Combine(desktopPath, $"{fileNameWithoutExtension}_{timeStamp}{fileExtension}");
            }

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 设置 LicenseContext

            using (ExcelPackage package = new ExcelPackage(new FileInfo(filePath)))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("AGV 任务");

                worksheet.Cells[1, 1].Value = "格口";
                worksheet.Cells[1, 2].Value = "物料编码";
                worksheet.Cells[1, 3].Value = "物料名称";
                worksheet.Cells[1, 4].Value = "数量";
                worksheet.Cells[1, 5].Value = "日期";
                worksheet.Cells[1, 6].Value = "类型";
                worksheet.Cells[1, 7].Value = "入库时间";

                int row = 2;
                foreach (var view in list)
                {
                    worksheet.Cells[row, 1].Value = view.BoxGridCode;
                    worksheet.Cells[row, 2].Value = view.MaterialCode;
                    worksheet.Cells[row, 3].Value = view.MaterialName;
                    worksheet.Cells[row, 4].Value = view.Quantity;
                    worksheet.Cells[row, 5].Value = view.CreatedDate.ToString();
                    worksheet.Cells[row, 6].Value = view.Type;
                    worksheet.Cells[row, 7].Value = view.PutInDate.ToString();

                  row++;
                }

                package.Save();
            }
        }
        public static void ExportLogToExcelMethod(List<View_Material_WarehouseLineDto> list)
        {
            //string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //string filePath = Path.Combine(desktopPath, "View_Log.xlsx");

            //// 检查文件是否已经存在，如果存在则添加时间戳以区分文件
            //if (File.Exists(filePath))
            //{
            //    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
            //    string fileExtension = Path.GetExtension(filePath);
            //    string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            //    filePath = Path.Combine(desktopPath, $"{fileNameWithoutExtension}_{timeStamp}{fileExtension}");
            //}

            //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 设置 LicenseContext

            //using (ExcelPackage package = new ExcelPackage(new FileInfo(filePath)))
            //{
            //    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("AGV 任务");

            //    worksheet.Cells[1, 1].Value = "工单";
            //    worksheet.Cells[1, 2].Value = "编码";
            //    worksheet.Cells[1, 3].Value = "日期";
            //    worksheet.Cells[1, 4].Value = "批次";
            //    worksheet.Cells[1, 5].Value = "数量";
            //    worksheet.Cells[1, 6].Value = "LPN";
            //    int row = 2;
            //    foreach (var view in list)
            //    {
            //        worksheet.Cells[row, 1].Value = view.OrderNo;
            //        worksheet.Cells[row, 2].Value = view.MaterialName;
            //        worksheet.Cells[row, 3].Value = view.DateTime.ToString();
            //        worksheet.Cells[row, 4].Value = view.Batch;
            //        worksheet.Cells[row, 5].Value = view.Size;
            //        worksheet.Cells[row, 6].Value = view.MaterialCode;
            //        row++;
            //    }

            //    package.Save();
            //}
        }
    }
}
